using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace SmartLauncher
{
	/// <summary>
	/// The icon engine centralises all applications icons that have been dynamically
	/// extracted from the executable files or the program associated to the extension.
	/// The image lists are linked to the listview on the main form
	/// </summary>
    static class IconEngine
    {
        static ImageList _smallImageList;
        static ImageList _largeImageList;
        static IconListManager _iconListManager;

        #region "Constructors"
        static IconEngine()
        {
            _smallImageList = new ImageList();
            _smallImageList.ColorDepth = ColorDepth.Depth32Bit;
            _smallImageList.ImageSize = new System.Drawing.Size(16, 16);

            _largeImageList = new ImageList();
            _largeImageList.ColorDepth = ColorDepth.Depth32Bit;
            _largeImageList.ImageSize = new System.Drawing.Size(32, 32);

            _iconListManager = new IconListManager(_smallImageList, _largeImageList);
        }
        #endregion

        #region "Procedures"
        public static void ClearLists()
        {
            _iconListManager.ClearLists();
        }

        public static int AddFileIcon(string filePath)
        {
            return _iconListManager.AddFileIcon(filePath);
        }

        public static ImageList SmallImageList
        {
            get { return _smallImageList; }
        }

        public static ImageList LargeImageList
        {
            get { return _largeImageList; }
        }
        #endregion

				/// <summary>
				/// Loads the application icon. If the name begins with "extern:" then the external file is loaded
				/// </summary>
				/// <returns></returns>
				public static Icon GetApplicationIcon()
				{
					Icon rc;
					string name;


					name = Properties.Settings.Default.Icon;
					try // If any error occurs, the default icon is returned
					{
						if (name.ToLower().StartsWith("extern:") == false)
						{
							rc = ((System.Drawing.Icon)Properties.Resources.ResourceManager.GetObject(name));
							// Icon not found, force default icon to be loaded
							if (rc == null) throw new ArgumentNullException();
						}
						else
						{
							rc = new Icon(name.Substring(7));
						}
					}
					catch
					{
						rc = Properties.Resources.icoSmartLauncher;
					}

					return rc;
				}

    }
}

